package com.platform.service.impl;
import com.google.common.collect.Lists;
import com.platform.base.BaseService;
import com.platform.common.Const;
import com.platform.common.ServerResponse;
import com.platform.pojo.User;
import com.platform.service.IMessageService;
import com.platform.util.DateTimeUtil;
import com.platform.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 消息服务层
 */
@Service("iMessageService")
public class MessageServiceImpl extends BaseService implements IMessageService {

    /**
     * 条件查询消息通知
     * @param paramMap
     * @param request
     */
    public ServerResponse<List<Map>> selectByParamMap(Map paramMap, HttpServletRequest request){
        //判断参数携带用户名是否是当前登陆用户名

        Object o = request.getSession().getAttribute(Const.CURRENT_USER);
        String userName = StringUtil.valueOfString(paramMap.get("username"));
        if(null != o){
            User user = (User)o;
            if(!userName.equals(user.getUsername())){
                return ServerResponse.createByErrorMessage(Const.Msg.PARAMTERERROR);
            }
        }
        List<Map> resultList = Lists.newArrayList();
        //查询即时发送的消息
        paramMap.put("noticetype",Const.NoticeType.TIMELY);
        List<Map> tList = messageMapper.selectByParamMap(paramMap);
        if(null != tList && 0 < tList.size()){
            //更新消息状态为失效
            paramMap.put("messagestatusvaules",Const.MessageStatus.INVALID);
            messageMapper.updateMessageStatusInId(paramMap);
            resultList.addAll(tList);
        }
        //查询时间区间发送的消息
        paramMap.put("noticetype",Const.NoticeType.INTERVAL);
        paramMap.put("noticetime",DateTimeUtil.dateToStr(new Date()));
        paramMap.put("expiretime",DateTimeUtil.dateToStr(new Date()));
        List<Map> dList = messageMapper.selectByParamMap(paramMap);
        if(null != dList && 0 < dList.size()){
            //更新消息状态为失效
            paramMap.put("messagestatus",Const.MessageStatus.INVALID);
            messageMapper.updateMessageStatusInId(paramMap);
            resultList.addAll(dList);
        }
        return ServerResponse.createBySuccess("查询成功",resultList);
    }

}
